package com.jilian.mydemo.modul.msg.ui;

import android.arch.lifecycle.Observer;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.Html;
import android.widget.TextView;

import com.jilian.mydemo.MyApplication;
import com.jilian.mydemo.R;
import com.jilian.mydemo.base.BaseActivity;
import com.jilian.mydemo.base.BaseDto;
import com.jilian.mydemo.base.BaseEventMsg;
import com.jilian.mydemo.common.dto.MsgDto;
import com.jilian.mydemo.common.dto.MsgRefreshDto;
import com.jilian.mydemo.event.RxBus;
import com.jilian.mydemo.factory.Factoty;
import com.jilian.mydemo.modul.main.viewmodel.MainViewModel;
import com.jilian.mydemo.utils.ToastUitl;

public class MsgDatailActivity extends BaseActivity {
    private MsgDto msgDto;
    private TextView tvMsgTitle;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        MyApplication.addActivity(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        MyApplication.removeActivity(this);
    }


    private TextView tvDate;
    private TextView tvContent;


    @Override
    protected void createViewModel() {

    }

    @Override
    public int intiLayout() {
        return R.layout.activity_msgdatail;
    }

    @Override
    public void initView() {
        setNormalTitle("消息详情", v -> finish());

        tvMsgTitle = (TextView) findViewById(R.id.tv_msg_title);
        tvDate = (TextView) findViewById(R.id.tv_date);
        tvContent = (TextView) findViewById(R.id.tv_content);

    }

    @Override
    public void initData() {
        read((MsgDto) getIntent().getSerializableExtra("data"));
    }

    /**
     * 读改消息
     *
     * @param msgDto
     */
    private void read(MsgDto msgDto) {
        if (msgDto == null) {
            return;

        }
        showLoadingDialog();
        Factoty.getViewModel(MainViewModel.class, MsgDatailActivity.this).read(msgDto.getId());
        Factoty.getViewModel(MainViewModel.class, MsgDatailActivity.this).getReadliveData().observe(this, new Observer<BaseDto>() {
            @Override
            public void onChanged(@Nullable BaseDto dto) {
                hideLoadingDialog();
                if (dto.isSuccess()) {
                    tvMsgTitle.setText(msgDto.getTitle());
                    tvContent.setText(Html.fromHtml(msgDto.getContent()));
                    tvDate.setText(msgDto.getCreateTime());
                    //通知刷新
                    //通知刷新UI
                    BaseEventMsg eventMsg = new BaseEventMsg();
                    MsgRefreshDto msgRefreshDto = new MsgRefreshDto();
                    msgRefreshDto.setCode(100);
                    eventMsg.setMsgRefreshDto(msgRefreshDto);
                    RxBus.getInstance().post(eventMsg);
                }
                else{
                    ToastUitl.showImageToastTips(dto.getMsg());
                }
            }
        });


    }

    @Override
    public void initListener() {

    }
}
